﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class MainForm : Form
    {
        private BGWC worker;
        public MainForm()
        {

            InitializeComponent();

        }



        private void startButton_Click(object sender, EventArgs e)
        {
            worker = new BGWC();
            worker.mainBackGroundWorker.ProgressChanged += ProgressChaned;
            worker.mainBackGroundWorker.RunWorkerCompleted += JobComplete;
            worker.Start();
            textBox1.Text += "Started" + Environment.NewLine;
        }

        private void JobComplete(object sender, RunWorkerCompletedEventArgs e)
        {
            textBox1.Text += "Finished. Canceled = " + e.Cancelled + Environment.NewLine;
        }

        private void ProgressChaned(object sender, ProgressChangedEventArgs e)
        {
            progressBar1.Value = e.ProgressPercentage;
        }

        private void stopButton_Click(object sender, EventArgs e)
        {
            worker.CancelWorker();
            textBox1.Text += "Canceled" + Environment.NewLine;
        }

        private void pauseButton_Click(object sender, EventArgs e)
        {
            worker.PauseWorker();
            textBox1.Text += "Paused" + Environment.NewLine;
        }

        private void continueButton_Click(object sender, EventArgs e)
        {
            worker.ContinueWorker();
            textBox1.Text += "Continued" + Environment.NewLine;
        }


    }

    class BGWC
    {
        ManualResetEvent _busy = new ManualResetEvent(true);
        public BackgroundWorker mainBackGroundWorker;

        public BGWC()
        {
            mainBackGroundWorker = new BackgroundWorker();
            mainBackGroundWorker.WorkerReportsProgress = true;
            mainBackGroundWorker.WorkerSupportsCancellation = true;
            mainBackGroundWorker.DoWork += mainBackGroundWorker_DoWork;
        }

        private void mainBackGroundWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            BackgroundWorker worker = sender as BackgroundWorker;
            for (int i = 0; i <= 100; i++)
            {
                _busy.WaitOne();
                if ((worker.CancellationPending == true))
                {
                    e.Cancel = true;
                    break;
                }
                worker.ReportProgress(i);
                Thread.Sleep(100);
            }  
        }

        public void Start()
        {
            mainBackGroundWorker.RunWorkerAsync();
        }

        public void CancelWorker()
        {
            //ContinueWorker();
            mainBackGroundWorker.CancelAsync();
        }

        public void PauseWorker()
        {
            if (mainBackGroundWorker.IsBusy)
            {
                _busy.Reset();
            }
        }

        public void ContinueWorker()
        {
            _busy.Set();
        }
    }
}
